import close from '@/utils/closeTag';
import resetPage from '@/utils/resetPage';
import dayjs from 'dayjs';
import emojiRegex from 'emoji-regex';
import { getColumnWidth } from '@/utils/el_table_content_width_adaptation';

export default {
	install(Vue) {
		// console.log("插件安装成功");
		// 定义全局指令
		Vue.directive('has', {
			inserted: (el, binding, vnode) => {
				const that = vnode.context;
				const permissionList = that.$store.getters.userInfo.permissionList || [];
				function permissionJudge(value) {
					return permissionList.includes(value);
				}
				if (!permissionJudge(binding.value)) {
					el.parentNode.removeChild(el);
				}
			}
		});
		Vue.directive('clearEmoji', {
			bind(el, binding, vnode, oldVnode) {
				const regex = emojiRegex();
				const obj = el.querySelectorAll('.el-input__inner,.el-textarea__inner')[0];
				let zclearNoNum = function (e) {
					if (e.target.composing) return;
					let match = regex.exec(obj.value); //也可以直接用正则表达式判断
					if (match) {
						/*for (let i=0;i<match.length;i++) {
							obj.value = obj.value.replace(match[i],"");
						}*/
						obj.value = obj.value.replace(/(\ud83c[\udc00-\udfff])|(\ud83d[\udc00-\udfff])|(\ud83e[\udc00-\udfff])|[\u2100-\u32ff]|[\u0030-\u007f][\u20d0-\u20ff]|[\u0080-\u00ff]/g, '');
						zclearNoNum(e);
					}
					// 触发v-model的更新
					obj.dispatchEvent(new Event('input'));
				};
				let zblur = function (e) {
					zclearNoNum(e);
					setTimeout(() => {
						let match = regex.exec(obj.value);
						if (match) {
							obj.value = obj.value.replace(/(\ud83c[\udc00-\udfff])|(\ud83d[\udc00-\udfff])|(\ud83e[\udc00-\udfff])|[\u2100-\u32ff]|[\u0030-\u007f][\u20d0-\u20ff]|[\u0080-\u00ff]/g, '');
							zclearNoNum(e);
						}
						// 触发v-model的更新
						obj.dispatchEvent(new Event('change'));
					}, 50);
				};
				obj.onkeyup = zclearNoNum;
				obj.onblur = zblur;

				function onCompositionStart(e) {
					e.target.composing = true;
				}
				function onCompositionEnd(e) {
					e.target.composing = false;
					obj.dispatchEvent(new Event('input'));
				}
				// https://segmentfault.com/a/1190000009246058
				obj.addEventListener('compositionstart', onCompositionStart);
				obj.addEventListener('compositionend', onCompositionEnd);
			},
			update(el, binding, vnode, oldVnode) {}
		});
		Vue.prototype.$pageSizes = [20, 50, 100, 200];
		Vue.prototype.elTableAdaptationWidth = getColumnWidth;
		Vue.prototype.resetPage = resetPage;
		Vue.prototype.$tokenName = '数字资产';
		Vue.prototype.$showFn = function (arr) {
			const permissionList = this.$store.getters.userInfo.permissionList || [];
			return arr.some(item => {
				return permissionList.includes(item);
			});
		};
		Vue.prototype.closeTag = close;
		Vue.prototype.trimAll = trimAll;
		Vue.prototype.$delFn = function (title, fn, row, data) {
			this.$confirm(`此操作将永久删除该${title}, 是否继续?`, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					fn(row, data);  
				})
				.catch(() => {});
		};

		Vue.filter('parseTime', function (value, str = 'YYYY-MM-DD HH:mm') {
			if (value) {
				const time = dayjs(value).format(str);
				if (time === 'Invalid Date') {
					return value;
				} else {
					return dayjs(value).format(str);
				}
			} else {
				return '';
			}
		});
		Vue.filter('fileTime', function (day, dateList) {
			for (const key in dateList) {
				if (key === day) {
					return dateList[key];
				}
			}
		});

		Vue.directive('height', {
			bind: function (el, bind) {
				// 只调用一次，指令第一次绑定到元素时调用。在这里可以进行一次性的初始化设置
			},
			inserted: function (el, bind) {
        // 被绑定元素插入父节点时调用 (仅保证父节点存在，但不一定已被插入文档中)
        // console.log('el', el);
				let parentNodesH = el.parentNode.clientHeight;
				let nodes = document.getElementById(el.id);
				let sibNodes = siblings(nodes);
				let exitH = 0;
				sibNodes.forEach(el => {
					exitH += el.clientHeight;
				});
				el.style.height = parentNodesH - exitH + 'px';
			}
		});
	}
};
function siblings(elm) {
	var a = []; //保存所有兄弟节点
	// console.log('elm', elm);
	var p = elm && elm.parentNode.children; //获取父级的所有子节点
	// console.log('p', p);
	for (var i = 0; i < p.length; i++) {
		//循环
		if (p[i].nodeType == 1 && p[i] != elm) {
			//如果该节点是元素节点与不是这个节点本身
			a.push(p[i]); // 添加到兄弟节点里
		}
	}
	return a;
}

function trimAll(ele) {
	if (typeof ele === 'string') {
		return ele.split(/[\t\r\f\n\s]*/g).join('');
	} else {
		console.error(`${typeof ele} is not the expected type, but the string type is expected`);
	}
}
